package com.sugon.quartz.calendar;

import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.AnnualCalendar;
import org.quartz.impl.calendar.HolidayCalendar;

import com.sugon.quartz.simpletrigger.SimpleJob;
import com.sugon.quartz.simpletrigger.SimpleJob2;

/**
 * @desc:
 * @author: luoy
 * @date: 2021-12-10 14:35
 */
public class CalendarTest {

    public static void main(String[] args) throws SchedulerException {

        //创建一个job
        JobDetail job2 = JobBuilder.newJob(SimpleJob2.class)
                .withIdentity("job2", "group2").build();

        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();



        // Add the holiday calendar to the schedule
        AnnualCalendar holidays = new AnnualCalendar();
            //这里设置日期 年月日
            //注意这里的一个坑，就是月份要比实际月份少一个月
            //2014,11,21 实际是2014年12月21日
        Calendar calendar = new GregorianCalendar(2020, 11, 10);
        holidays.setDayExcluded(calendar, true);
            // tell the schedule about our holiday calendar
        scheduler.addCalendar("holidays", holidays, false, false);

        //注册trigger并启动scheduler
        scheduler.scheduleJob( job2,get2());
        scheduler.start();

    }


    public  static SimpleTrigger get2(){
        //指定时间触发，每隔10秒执行一次，重复10次：
        SimpleTrigger trigger = (SimpleTrigger) newTrigger()
                .withIdentity("trigger3", "group3")
                .startAt(new Date())  // if a start time is not given (if this line were omitted), "now" is implied
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(10)
                        .withRepeatCount(10)) // note that 10 repeats will give a total of 11 firings
                .forJob("job2","group2") // identify job with handle to its JobDetail itself
                .modifiedByCalendar("holidays")
                .build();

        return trigger;
    }
}
